package org.example.quartz.controller;


import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.db.ds.simple.SimpleDataSource;
import lombok.extern.slf4j.Slf4j;
import org.example.quartz.service.UtilService;
import org.example.quartz.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.Map;

@Slf4j
@RestController("gsControllerSlave1")
@RequestMapping(path = "/GSYY/GsController")
public class GsController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value(value = "${spring.datasource.databaseType}")
    private String databaseType;
    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    @Value(value = "${spring.datasource.GSYY}")
    private String GSYY;
    @Autowired
    private UtilService utilService;


    /**
     * 修改
     *
     * @param param
     * @return
     */
    @PostMapping("/updateGsecc")
    public Result updateGsecc(@RequestBody Map<String, Object> param) {
        DataSource ds = new SimpleDataSource(jdbcUrl, username, password);
        try (Session session = Session.create(ds)) {
            if ("oracle".equals(databaseType)||"dm".equals(databaseType)) {
                Entity entity = Entity.create(utilService.getGSYY()+".gs_ecc");
                for (Map.Entry<String, Object> stringObjectEntry : param.entrySet()) {
                    entity.set(stringObjectEntry.getKey(), stringObjectEntry.getValue());
                }
                session.update(entity, Entity.create(utilService.getGSYY()+".gs_ecc").set("id", 1));
                return Result.success("修改成功");
            } else {
                String updateSql = "UPDATE "+utilService.getGSYY()+".gs_ecc SET IP = '" + param.get("ip") + "', " +
                        "PORT = '" + param.get("port") + "', " +
                        "ECC = '" + param.get("ecc") + "', " +
                        "Bearer = '" + param.get("bearer") + "', " +
                        "TYPE = '" + param.get("type") + "' " +
                        "WHERE id = 1\n";
                jdbcTemplate.update(updateSql);
                return Result.success("修改成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @PostMapping("/getById")
    public Result getById(@RequestParam("id") Integer id) {
        try {
            Map<String, Object> map = jdbcTemplate.queryForMap("select * from "+GSYY+".gs_ecc where  id = ?", id);
            return Result.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

}
